package com.ywl.study.sharding.study02;

import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class SpringDataSourceTest {

    @Autowired
    DataSource dataSource;

    @PostConstruct
    public void search() throws SQLException {


//        PreparedStatement statement=dataSource.getConnection().prepareStatement("SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?");
//        statement.setInt(1,1000);
//        statement.setInt(2,1);


//        PreparedStatement statement=dataSource.getConnection().prepareStatement("SELECT  * FROM t_order_item WHERE  order_id=?");
//       statement.setInt(1,1);
//        ResultSet resultSet=statement.executeQuery();
//        HintManager hintManager=HintManager.getInstance();
//
//        while(resultSet.next()){
//            System.out.println(resultSet.getInt(1));
//            System.out.println(resultSet.getString(2));
//        }

        PreparedStatement statement=dataSource.getConnection().prepareStatement("SELECT  * FROM t_order ");
        //这个hitManager不起作用，不知道为啥
//        HintManager hintManager=HintManager.getInstance();
//        hintManager.addTableShardingValue("t_order_item","t_order_item0");
        ResultSet resultSet=statement.executeQuery();

//        while(resultSet.next()){
//            System.out.println(resultSet.getInt(1));
//            System.out.println(resultSet.getString(2));
//        }
    }
}
